# encoding: utf-8
# @author: luzhangdan
# @file: conftest.py
# @time: 2021/10/19 15:27

import pytest
from api.base_api import BaseApi
from api.manage.login_api import LoginApi
from api.manage.getSession_api import GetSessionApi
from common.parse import get_jsonpath_value


def pytest_collection_modifyitems(items):
    """
    对pytest运行中的用例名称重新编码
    :param items: 所有测试用例
    :return:
    """
    for item in items:
        # 对item的name和nodeid进行重新编码和解码
        item.name = item.name.encode().decode('unicode_escape')
        item._nodeid = item.nodeid.encode().decode('unicode_escape')


# 定义fixture，实现对登录接口发起请求，并且提取token值
# session module class function(函数、方法)
@pytest.fixture(scope='session', autouse=True)
def get_login_token():
    session = GetSessionApi()
    session_response = session.send()
    login_session = get_jsonpath_value(session_response.text, '$.data')
    login = LoginApi(login_session)
    resp = login.send()
    # 把token作为一个类属性
    BaseApi.login_token = get_jsonpath_value(resp.text, '$.data.access_token')

